/**
 * Copyright 1998-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
package ca.uvic.cs.cogz.mapping.ui.tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import ca.uvic.cs.cogz.mapping.filter.TreeFilterManager;
import ca.uvic.cs.cogz.util.FrameUtil;
import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.DefaultCls;
import edu.stanford.smi.protege.model.Frame;
import edu.stanford.smi.protege.model.FrameID;
import edu.stanford.smi.protege.model.Slot;
import edu.stanford.smi.protege.ui.ParentChildNode;
import edu.stanford.smi.protege.ui.SlotSubslotRoot;
import edu.stanford.smi.protege.util.LazyTreeNode;

/**
 * Extends {@link ParentChildNode} to retrieve only interesting children when the filter is applied.
 * 
 * @author Chris Callendar, Sean Falconer
 */
public class FilteredParentChildNode extends ParentChildNode implements IFilteredTreeNode {
	private boolean mappingFiltered; 
	private int numOfChildren = -1;
	
	private List<Frame> slots;

	public FilteredParentChildNode(LazyTreeNode parentNode, Cls cls) {
		super(parentNode, cls);
	}

	@Override
	protected LazyTreeNode createNode(Object o) {
		if(o instanceof DefaultCls) {
			Cls temp = (DefaultCls)o;
			
			/*if(temp.getFrameID().getName().equals(Integer.MAX_VALUE + "")) {
				if(this.getParent() instanceof FilteredParentChildNode) {
					return new SlotSubslotRoot(temp.getKnowledgeBase(), slots);
				}
			}*/
		}
		return new FilteredParentChildNode(this, FrameUtil.getCls(o));
	}

	@Override
	protected int getChildObjectCount() {
		return (isFiltered() ? getFilterChildrenCount() : getAllChildrenCount());
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Collection getChildObjects() {
		if (isFiltered()) {
			return FrameUtil.getSearchMatchedFrames(super.getChildObjects());
		}

		//return super.getChildObjects();
		
		Collection children = new ArrayList();
		
		// hack for adding dummy node for holding slots associated with this Cls
		Cls currentCls = (Cls) (this.getUserObject());
		children.addAll(super.getChildObjects());
		
		// add slots as children to this Cls tree node
		/*if(!(this.getParent() instanceof FilteredParentChildRoot)) {
			DefaultCls temp = new DefaultCls(currentCls.getKnowledgeBase(), new FrameID(Integer.MAX_VALUE + ""));
			
			// get the slots and outgoing relationships for this class
			slots = new ArrayList<Frame>();
			slots.addAll(FrameUtil.getDomainRangeSlots(currentCls));
			
			if(slots.size() > 0) {
				children.add(temp);
			}
		}*/
		
		numOfChildren = children.size();

		return children;
	}
	
	public String getToolTipText() {
		return ((Frame) this.getUserObject()).getBrowserText();
	}

	@Override
	public String toString() {
		return "Filtered" + super.toString();
	}

	// IFilteredTreeNode methods 	
	public boolean isFiltered() {
		return TreeFilterManager.getInstance().isTreeFilteringActive();
	}

	public int getFilterChildrenCount() {
		return FrameUtil.getSearchMatchedFrames(super.getChildObjects()).size();
	}

	public int getAllChildrenCount() {
		if(numOfChildren == -1) {
			numOfChildren = getChildObjects().size();
		}
		return numOfChildren;
	}

	public Object getUserObject() {
		Object o = super.getUserObject();

		return o;
	}

	public void setMappingFiltered(boolean mappingFiltered) {
		this.mappingFiltered = mappingFiltered;
	}

	public boolean isMappingFiltered() {
		return mappingFiltered;
	}
}
